import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf
import numpy as np

'''
to calculate the false nagetive, false positive for REDCAPSpatial

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey


#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    aatfile = 'C:/AZTool/test/NEUS.csv'
    aat = build_data_list(aatfile)
    #print aat
    ctgfile = 'C:/Documents and Settings/wang322/My Documents/test/satscan/NortheeaternUS.ctg'
    ctg = build_data_list(ctgfile)
    #print ctg
    idstr = []
    for item in aat:
        if len(str(int(item[0]))) < 2:
            temp_a = '00' + str(int(item[0]))
        elif len(str(int(item[0]))) < 3:
            temp_a = '0' + str(int(item[0]))
        else:
            temp_a = str(int(item[0]))
            
        if len(str(int(item[1]))) < 2:
            temp_b = '00' + str(int(item[1]))
        elif len(str(int(item[1]))) < 3:
            temp_b = '0' + str(int(item[1]))
        else:
            temp_b = str(int(item[1]))
        idstr.append(temp_a+temp_b)
        idstr.append(temp_b+temp_a)
    #print idstr
    peri_mean = np.mean(aat[:,2])

    appendList = []
    for item in ctg:
        if int(item[0]) <> int(item[1]):
            if len(str(int(item[0]))) < 2:
                temp_a = '00' + str(int(item[0]))
            elif len(str(int(item[0]))) < 3:
                temp_a = '0' + str(int(item[0]))
            else:
                temp_a = str(int(item[0]))
                
            if len(str(int(item[1]))) < 2:
                temp_b = '00' + str(int(item[1]))
            elif len(str(int(item[1]))) < 3:
                temp_b = '0' + str(int(item[1]))
            else:
                temp_b = str(int(item[1]))
            tempStr = temp_a+temp_b
            if tempStr not in idstr:
                appendList.append([item[0], item[1], peri_mean])
                #appendList.append([item[1], item[0], peri_mean])
    print appendList
    filePath = 'c:/temp.txt'
    np.savetxt(filePath, appendList, delimiter=',', fmt = '%10.5f')
    #print np.mean(output[:,9]), np.mean(output[:,10]), np.mean(output[:,11])
    #filePath = 'C:/_DATA/CancerData/test/Jan15/spatialREDCAP/result_new.csv'
    #np.savetxt(filePath, output, delimiter=',', fmt='%4e')
    print "end at " + getCurTime()
    print "========================================================================"  